<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/page/public/public-taglibs.jsp"%>
<!DOCTYPE html>

<html lang="en">
<head>
<%@ include file="/page/public/public-css.jsp"%>
</head>
<body class="no-skin">
	<div class="main-container sub-page-content" id="main-container">
		<!-- /section:basics/sidebar -->
		<div class="main-content">
			<div class="main-content-inner">
				<div class="page-content">
					<div class="row">
						<!-- /.page-header -->
						<div class="col-xs-4 col-sm-4 widget-container-col">
							<div class="widget-box widget-color-green">
								<!-- #section:custom/widget-box.options -->
								<div class="widget-header">
									<h5 class="widget-title">
										<i class="ace-icon fa fa-users bigger-130"></i>角色列表
									</h5>

									<div class="widget-toolbar no-border">
										<input type="text" class="input-sm" id="search"
											placeholder="查找..." style="width: 100px"> <span
											style="padding-left: 20px;" data-rel="tooltip"
											data-placement="bottom" title="刷新" onclick="initJsTree();">
											<i class="ace-icon fa fa-refresh bigger-120 "></i>
										</span>
									</div>

								</div>

								<!-- /section:custom/widget-box.options -->
								<div class="widget-body"
									style="min-height: 450px; max-height: 500px; overflow-y: auto">
									<div class="widget-main">
										<div class="clearfix">
											<div class="row">
												<div class="col-xs-12">
													<shiro:hasPermission name="role:create">
														<button class="btn btn-xs btn-yellow"
															onclick="toCreateSelectedSubRole();">
															<i class="ace-icon fa fa-plus icon-on-left"></i>新增角色
														</button>
													</shiro:hasPermission>
													<shiro:hasPermission name="role:update">
														<button class="btn btn-xs btn-yellow"
															onclick="toEditSelectedRole();">
															<i class="ace-icon fa fa-edit icon-on-left"></i>修改角色
														</button>
													</shiro:hasPermission>
													<shiro:hasPermission name="role:delete">
														<button class="btn btn-xs btn-yellow"
															onclick="toDeleteSelectedRole();">
															<i class="ace-icon fa fa-trash-o icon-on-left"></i>删除角色
														</button>
													</shiro:hasPermission>
												</div>

												<div class="col-xs-12">
													<div class="hr hr-dotted hr-16"></div>
													<div id="jstree"></div>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
						<!-- /.span -->
						<!-- /.page-header -->
						<div class="col-xs-8 col-sm-8 widget-container-col"
							style="padding-left: 0px;">
							<div class="row">
								<div class="col-xs-12 col-sm-12">


									<div class="widget-box widget-color-green">
										<!-- #section:custom/widget-box.options -->
										<div class="widget-header">
											<h5 class="widget-title">
												<i class="ace-icon fa fa-info bigger-130"></i>角色资源权限
											</h5>
											<div class="widget-toolbar no-border"></div>
										</div>

										<!-- /section:custom/widget-box.options -->
										<div class="widget-body" style="min-height: 450px;">
											<div class="widget-main">
												<div class="clearfix">

													<!-- PAGE CONTENT BEGINS -->
													<div class="row">
														<div class="col-xs-12">
															<shiro:hasPermission name="role:update">
																<button class="btn btn-xs btn-yellow  pull-right"
																	id="btnCancelResource" disabled
																	onclick="toCancelEditResourceTree();">
																	<i class="ace-icon fa fa-undo icon-on-left"></i>还原
																</button>
																<button class="btn btn-xs btn-yellow  pull-right"
																	style="margin-right: 5px;" id="btnSaveResource"
																	disabled onclick="toSaveResourceTree();">
																	<i class="ace-icon fa fa-save icon-on-left"></i>保存
																</button>
																<button class="btn btn-xs btn-yellow  pull-right"
																	style="margin-right: 5px;" disabled
																	onclick="toEnableResourceTree();" id="btnEditResource">
																	<i class="ace-icon fa fa-edit icon-on-left"></i>编辑
																</button>
															</shiro:hasPermission>

															&nbsp;&nbsp;&nbsp; <span class="pull-left"> <span
																class="btn-toolbar inline middle no-margin"> <span
																	id="select2-multiple-style" data-toggle="buttons"
																	class="btn-group no-margin"> <label
																		class="green" style="font-size: 16px"
																		id="selectedRole"> </label>
																</span>
															</span>
															</span>

														</div>
														<div class="col-xs-12">
															<div class="hr hr-dotted hr-16"></div>
															<div id="resourceTree"></div>
														</div>
														<!-- /.span -->
													</div>
													<!-- /.row -->
												</div>
											</div>
										</div>
									</div>

								</div>
							</div>
						</div>


					</div>
					<!-- /.row -->
				</div>
			</div>
		</div>
	</div>




	<!-- 角色编辑（新增、修改）模态框（Modal） -->
	<div class="modal fade" id="modal-role" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content" id="modal-role-content"></div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->


	<!-- 角色编辑（新增、修改）JS模板 -->
	<script id="tmpl-edit-role" type="text/x-jquery-tmpl">

				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true" style="font-size: 30px">×</button>
						<h4 class="modal-title">编辑角色</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-xs-12">
							<!-- PAGE CONTENT BEGINS -->
							<form id="roleForm" class="form-horizontal" role="form">
								<!-- #section:elements.form -->

								<div class="space-4"></div>
								<input type="hidden"  name="role.id" value="{{= id}}" /> 
								<input type="hidden"  name="role.organization_id" value="{{= organization_id}}" /> 
								
							   <div class="form-group">
									<label class="col-sm-3 control-label no-padding-right" > 角色名称 </label>
									<div class="col-sm-9">
										<input type="text"  name="role.role"  class="col-xs-12 col-sm-12"  value="{{= role }}" />
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label no-padding-right" > 备注</label>
									<div class="col-sm-9">
										<textarea class="form-control limited" name="role.description" maxlength="50" rows="5">{{= description }}</textarea>
									</div>
								</div>
								<div class="space-4"></div>
								<div class="clearfix form-actions">
									<div class="col-md-offset-3 col-md-9">
										<button class="btn btn-info" type="button"
											onclick="saveRole();">
											<i class="ace-icon fa fa-check"></i> 保存
										</button>

										&nbsp; &nbsp; &nbsp;
										<button class="btn" type="button"
											onclick="hideModalRole();">
											<i class="ace-icon fa fa-undo"></i> 取消
										</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
	</script>

	<%@ include file="/page/public/public-js.jsp"%>
	<script type="text/javascript">
		$(function() {
			initJsTree();//初始化组织机构树
			bindResizeEvent();//绑定页面resize事件
		});

		function initJsTree() {
			var url = ctx + "/system/role/loadOrganizationTreeData";
			$.ajax({
				url : url,
				type : 'POST',
				dataType : 'json',
				success : function(response) {
					if (response.status == RESULT_SUCCESS) {
						treeConfig(response.data.treeNodes);
					} else {
						handleAjaxError(response);
					}
				}
			});
		}

		function resourceTreeConfig(data, canEdit) {
			if ($('#resourceTree').jstree(true)) { //销毁当前资源权限树，
				$('#resourceTree').jstree(true).destroy();
			}

			//构造资源权限树
			var $jstree = $('#resourceTree')
					.jstree(
							{
								"core" : {
									"animation" : 0,
									"check_callback" : true,
									"data" : data
								},
								/**'force_text' : true,**/
								'themes' : {
									'responsive' : false,
									'variant' : 'large',
									'stripes' : true
								},
								"types" : {
									"#" : {
										"max_depth" : 4,
										"valid_children" : [ "root", "menu",
												"button" ]
									},
									"root" : {
										"icon" : "ace-icon fa fa-home bigger-130 blue"
									},
									"menu" : {
										"icon" : "ace-icon fa fa-leaf bigger-130 green"
									},
									"button" : {
										"icon" : "ace-icon fa  fa-hand-pointer-o bigger-130 blue"
									},
									"default" : {
										"icon" : "ace-icon fa fa-file icon-lg blue"
									},
									"file" : {
										"icon" : "ace-icon fa fa-file icon-lg blue"
									}
								},
								"checkbox" : {
									//"three_state":true,
									"cascade" : false,
									"keep_selected_style" : false,
								//"real_checkboxes" : true  
								},
								"plugins" : [ "checkbox", "types", "wholerow"
								// "wholerow", "dnd"  这两种插件可以不要,checkbox可以使用
								]
							}).on("ready.jstree", function(e) {
					});
		}

		function treeConfig(data) {
			
			if($('#jstree').jstree(true)){
				$('#jstree').jstree(true).destroy();
			}
			
			var to = false;
			$('#search').keyup(function() {
				if (to) {
					clearTimeout(to);
				}
				to = setTimeout(function() {
					var v = $('#search').val();
					$('#jstree').jstree(true).search(v);
				}, 250);
			});

			$jstree = $('#jstree')
					.jstree(
							{
								"core" : {
									"animation" : 0,
									"check_callback" : true,
									"data" : data
								},
								'themes' : {
									'responsive' : false,
									'variant' : 'large',
									'stripes' : true
								},
								"types" : {
									"#" : {
										"max_depth" : 3,
										"valid_children" : [ "company",
												"branch", "role" ]
									},
									"company" : {
										"icon" : "ace-icon fa fa-institution bigger-130 red2"
									},
									"branch" : {
										"icon" : "ace-icon fa fa-sitemap bigger-130 blue"
									},
									"role" : {
										"icon" : "ace-icon fa ace-icon fa fa-user bigger-130 orange"
									},
									"default" : {
										"icon" : "ace-icon fa fa-file icon-lg blue"
									},
									"file" : {
										"icon" : "ace-icon fa fa-file icon-lg blue"
									}
								},
								"plugins" : [ "contextmenu", "search",
										"unique", "conditionalselect", "state",
										"types", "changed"
								// "wholerow", "dnd"  这两种插件可以不要,checkbox可以使用
								],
								"conditionalselect" : function(node, event) {
									return true;
								},
								"contextmenu" : {
									"items" : {
										"create" : null,
										"rename" : null,
										"remove" : null,
										"ccp" : null,
										/**
										    separator_before - a boolean indicating if there should be a separator before this item
										    separator_after - a boolean indicating if there should be a separator after this item
										    _disabled - a boolean indicating if this action should be disabled
										    label - a string - the name of the action (could be a function returning a string)
										    action - a function to be executed if this item is chosen
										    icon - a string, can be a path to an icon or a className, if using an image that is in the current directory use a ./ prefix, otherwise it will be detected as a class
										    shortcut - keyCode which will trigger the action if the menu is open (for example 113 for rename, which equals F2)
										    shortcut_label - shortcut label (like for example F2 for rename)
										 **/
										"add" : {
											"label" : "新增角色",
											"icon" : "ace-icon fa fa-folder bigger orange",
											"_disabled" : false,
											"separator_before" : false,
											"separator_after" : false,
											"action" : function(obj) {
												toCreateSelectedSubRole();
											}
										},
										"edit" : {
											"label" : "修改角色",
											"_disabled" : false,
											"separator_before" : true,
											"separator_after" : false,
											"icon" : "ace-icon fa fa-edit icon-lg yellow",
											"action" : function(obj) {
												toEditSelectedRole();
											}
										},
										"delete" : {
											"label" : "删除角色",
											"_disabled" : false,
											"separator_before" : true,
											"separator_after" : false,
											"icon" : "ace-icon fa fa-trash-o icon-lg red2",
											"action" : function(obj) {
												//var inst = jQuery.jstree.reference(obj.reference);
												//var clickedNode = inst.get_node(obj.reference);
												toDeleteSelectedRole();
											}
										}

									}
								}
							});

			$jstree.on("changed.jstree", function(e, data) {
				var action = data.action;
				if (action == "select_node") {
					var roleId = data.node.id;
					var dictType = data.node.type;

					if (dictType == 'company' || dictType == 'branch') {
						//加载机构下的角色列表
						loadSubRoleList(roleId);
						//清空右侧栏的角色资源权限树
						if ($('#resourceTree').jstree(true)) {
							$('#resourceTree').jstree(true).destroy();
						}
						//情况当前角色信息
						$("#selectedRole").html("");

						//角色权限操作按钮不可用
						$("#btnEditResource").attr("disabled", true);
						$("#btnSaveResource").attr("disabled", true);
						$("#btnCancelResource").attr("disabled", true);
					} else if (dictType == 'role') {
						var is_fixed = data.node.original.fasDatas.is_fixed;
						//加载角色资源权限树
						var canEdit = true;
						if (is_fixed == 1)
							canEdit = false;
						loadRoleOfResourceTree(roleId, canEdit);
					}
				}
			}).on("show_contextmenu.jstree", function(e, data) {
				var dictType = data.node.type;
				if (dictType == 'company' || dictType == 'branch') {//公司、分公司节点不显示“修改角色”、“删除角色”右键菜单
					$(".jstree-contextmenu").find("li:gt(1)").hide();
				} else if (dictType == 'role') { //角色节点，不显示“修改角色”、“删除角色”右键菜单
					$(".jstree-contextmenu").find("li:lt(1)").hide();
				}
			}).on("loaded.jstree", function(e) {
			}).on("init.jstree", function(e) {
			}).on("loading.jstree", function(e) {
			}).on("ready.jstree", function(e) {
				//角色树全部加载完成后,默认选中根节点,查询出根节点机构的角色列表
				var rootNode = $('#jstree').jstree(true).get_node("#");
				var firstId = rootNode.children[0];
				$('#jstree').jstree(true).select_node(firstId);
				loadSubRoleList(firstId);
			});

		}

		/**
		 *角色资源权限编辑面板--所有按钮不可用（编辑、保存、还原）
		 *			点击“保存”成功后,禁止编辑，需点击“编辑”按钮才行
		 */
		function disabledRoleResourceBtn() {
			$("#btnEditResource").attr("disabled", true);
			$("#btnSaveResource").attr("disabled", true);
			$("#btnCancelResource").attr("disabled", true);
		}

		/**
		 *角色资源权限面板- 资源树可不用 
		 		（主要用于角色资源权限保存成功后）
		 */
		function disabledRoleResourceTree() {
			var ref = $('#resourceTree').jstree(true);
			ref.load_all(null, function(node, obj) {
				var allNodes = node.children_d;
				$.each(allNodes, function(i, value) {
					ref.disabled_node(this);
				});
			});
		}

		/**
		 *获取角色树，当前选中节点Node(可能机构、角色,根据Node的type区分)
		 *
		 * 根据业务场景,暂时只考虑单选情况，返回选中数组[0]
		 */
		function getRoleTreeSelectedNode() {
			var ref = $('#jstree').jstree(true);
			var sel = ref.get_selected();
			if (!sel.length) {
				top.bootbox.alert("请先选择一个节点");
				return false;
			}
			sel = sel[0];
			return ref.get_node(sel);
		}

		/**
		 *  加载角色资源权限树（当点击右侧角色节点，根据角色ID，查询加载角色对应的资源权限）
		 *    treeNodeId: 被点击角色的node.id ,格式：R_+roleid
		 *	  canEdit:是否可以编辑(true|false)  如果角色is_fixed（锁定），则不允许编辑
		 */
		function loadRoleOfResourceTree(treeNodeId, canEdit) {
			disabledRoleResourceBtn();
			if (canEdit) {
				$("#btnEditResource").removeAttr("disabled");
			}

			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}

			$("#selectedRole").html(selectedNode.text);

			var url = ctx + "/system/role/loadResourceTreeDataByRole";
			url = addParam(url, "treeNodeId", treeNodeId);
			$.ajax({
				url : url,
				type : 'POST',
				dataType : 'json',
				success : function(response) {
					if (response.status == RESULT_SUCCESS) {
						resourceTreeConfig(response.data.treeNodes, canEdit);
					} else {
						handleAjaxError(response);
					}
				}
			});

		}

		/**
		 * 角色资源权限面板- 资源树可用
		 *		点击角色资源权限面板 "编辑"按钮情况
		 *   1) 所有资源节点可用
		 *   2) “保存”，“还原”按钮可用， “编辑”按钮不可用
		 */
		function toEnableResourceTree() {
			$('#resourceTree').jstree(true).load_all(null, function(node, obj) {
				var allNodes = node.children_d;
				$.each(allNodes, function(i, value) {
					$('#resourceTree').jstree(true).enable_node(this);
				});

				$("#btnEditResource").attr("disabled", true);
				$("#btnSaveResource").removeAttr("disabled");
				$("#btnCancelResource").removeAttr("disabled");
			});
		}

		/**
		 *  角色资源权限面板- 还原功能
		 *		点击角色资源权限面板 "还原"按钮情况
		 *   1) 根据选中角色node.id，重新加载角色资源树（不可用）
		 *   2) “保存”，“还原”按钮不可用， “编辑”按钮可用
		 */
		function toCancelEditResourceTree() {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}

			loadRoleOfResourceTree(selectedNode.id, true);

			$("#btnEditResource").removeAttr("disabled");
			$("#btnSaveResource").attr("disabled", true);
			$("#btnCancelResource").attr("disabled", true);
		}

		/**
		 * 角色资源权限保存确认
		 *        当点击角色资源权限面板，“保存”按钮时，提示是否确认信息
		 *   1) 提示确认信息
		 *   2) 执行保存操作
		 */
		function toSaveResourceTree() {
			top.bootbox.confirm("确定要保存修改过后的角色资源权限.", function(result) {
				if (result) {
					doSaveResourceTree();
				}
			});
		}

		/**
		 * 角色资源权限保存
		 *      当点击角色资源权限面板，“保存”按钮时，弹出确认信息后点击确定
		 */
		function doSaveResourceTree() {
			var ref = $('#resourceTree').jstree(true);

			//1、获取选中的资源权限node.id,(节点状态为undetermined和checked都被认为是选中)
			var resourceIds = "";
			ref.load_all(null, function(node, obj) {
				var allNodes = node.children_d;
				$.each(allNodes, function(i, value) {
					var node = ref.get_node(this);
					if (ref.is_undetermined(node) || ref.is_checked(node)) {
						resourceIds += node.id + ",";
					}
					//console.log(node.text +" is_undetermined = "+ref.is_undetermined (node) +" is_checked="+ref.is_checked (node) );
				});
			});
			//console.log(resourceIds);

			//获取当前选中的角色节点
			var selectedRoleNode = getRoleTreeSelectedNode();
			if (!selectedRoleNode) {
				return false;
			}
			var url = ctx + "/system/role/batchSaveRoleOfResource";
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"url" : url,
				"data" : {
					"treeNodeId" : selectedRoleNode.id,
					"resourceIds" : resourceIds
				},
				"success" : function(response) {
					if (response.status == RESULT_SUCCESS) {
						//保存成功后，重新加载角色资源权限树
						loadRoleOfResourceTree(selectedRoleNode.id, true);
					} else {
						handleAjaxError(response);
					}
				}
			});
		}

		/**
		 *  根据机构ID，加载机构下定义的直属角色列表
		 *			当点击角色树的公司、或者分公司节点时加载
		 *   orgId: 机构ID
		 */
		function loadSubRoleList(orgId) {
			var url = ctx + "/system/role/loadRolesByOrgId";
			url = addParam(url, "orgId", orgId);
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"url" : url,
				"success" : function(response) {
					if (response.status == RESULT_SUCCESS) {
						if (response.data.treeNodes) {
							var nodes = response.data.treeNodes;
							var ref = $('#jstree').jstree(true);
							var selectedOrgNode = getRoleTreeSelectedNode();
							if (!selectedOrgNode) {
								return false;
							}

							//将角色节点，创建在选中公司节点下
							$.each(nodes, function() {
								ref.create_node(selectedOrgNode.id, this);
							});
						}
					} else {
						handleAjaxError(response);
					}
				}
			});
		}

		/**
		 * 在当前选中机构下，新增角色
		 *			当点击角色面板,"创建角色"按钮时
		 *   1) 新增校验。  角色节点不能创建角色
		 *   2) 构建新增角色的初始化属性
		 *   3) 弹出角色编辑模态窗口
		 */
		function toCreateSelectedSubRole() {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}
			var dictType = selectedNode.type;
			if (dictType == 'role') {
				top.bootbox.alert("角色下面不能新增角色");
				return false;
			}

			//构建角色初始化属性 ，角色organization_id，为当前选中公司机构id
			var data = {
				"role" : "",
				"organization_id" : selectedNode.id
			};

			showModalRole(data);
		}

		/**
		 *  根据角色node.id加载角色信息，并弹出编辑窗口
		 *      roleId: 当前选中角色节点node.id(并发roleid,角色node.id规则为：R_+roleid)
		 */
		function toEditRoleById(treeNodeId) {
			var url = ctx + "/system/role/findRoleById";
			url = addParam(url, "treeNodeId", treeNodeId);
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"url" : url,
				"success" : function(response) {
					if (response.status == RESULT_SUCCESS) {
						showModalRole(response.data);
					} else {
						handleAjaxError(response);
					}
				}
			});
		}

		/**
		 * 编辑树形选中角色
		 *     1\当角色节点，右键菜单点击"编辑"时 2\点击"编辑角色"按钮后
		 *  1) 是否锁定编辑校验： 
		 *  2) 根据角色node.id加载角色信息，并弹出编辑窗口
		 */
		function toEditSelectedRole() {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}
			var is_fixed = selectedNode.original.fasDatas.is_fixed;
			if (is_fixed == 1) {//如果锁定，则不允许编辑
				top.bootbox.alert(selectedNode.text + "已锁定，不能编辑|删除");
				return false;
			}

			//根据角色node.id加载角色信息，并弹出编辑窗口
			toEditRoleById(selectedNode.id);
		}

		/**
		 * 弹出角色编辑窗口
		 *   1) 是否选中校验
		 *   2) 角色编辑页面初始化，使用jquery模版
		 *   3) 初始化表单校验
		 *   4) 弹出模态窗口
		 */
		function showModalRole(data) {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}

			var roleHtml = $("#tmpl-edit-role").tmpl(data);
			$('#modal-role-content').html(roleHtml);

			//初始化表单校验
			initFormValidator();

			//弹出新增窗口
			$('#modal-role').modal({
				keyboard : true
			});
		}

		/**
		 * 隐藏角色编辑窗口
		 */
		function hideModalRole() {
			$('#modal-role').modal('hide');
		}

		/**
		 * 保存角色 
		 *		（新增角色 | 修改角色）在角色编辑模态窗口，当点击“保存”按钮 ，
		 *  1) 是否选中校验
		 *  2) 表单校验
		 *  3) 表单序列化
		 *  4) 保存
		 */
		function saveRole() {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}

			if (!$('#roleForm').valid()) {
				return false;
			}

			var url = ctx + "/system/role/saveRole";
			var data = $('#roleForm').serializeObject();
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"url" : url,
				"data" : data,
				"success" : function(response) {
					if (response.status == RESULT_SUCCESS) {
						var ref = $('#jstree').jstree(true);
						var newNode = response.data;
						if (data['role.id']) { //如果id不为空则认为是修改操作
							ref.rename_node(selectedNode, response.data.text);
						} else { //创建
							ref.create_node(selectedNode.id, newNode);
						}
						hideModalRole();
					} else {
						handleAjaxError(response);
					}
				}
			});
		}

		/**
		 * 尝试删除选中角色
		 *			当点击“删除角色”按钮时
		 *  1)  是否选中校验 
		 *  2)  是否锁定校验
		 *  3)  执行删除
		 */
		function toDeleteSelectedRole() {
			var selectedNode = getRoleTreeSelectedNode();
			if (!selectedNode) {
				return false;
			}
			var is_fixed = selectedNode.original.fasDatas.is_fixed;
			if (is_fixed == 1) {
				top.bootbox.alert(selectedNode.text + "已锁定，不能编辑|删除");
				return false;
			}

			doDeleteRole(selectedNode.id);
		}

		/**
		 * 删除角色对象
		 *	 treeNodeId: 当前选中角色node.id
		 */
		function doDeleteRole(treeNodeId) {
			top.bootbox.confirm("确定要删除? 删除后可能影响程序正常运行！请确认记录未关联使用.", function(
					result) {
				if (result) {
					var url = ctx + "/system/role/deleteRole";
					url = addParam(url, "treeNodeId", treeNodeId);
					$.ajax({
						type : 'POST',
						url : url,
						dataType : "json",
						success : function(response) {
							if (response.status == RESULT_SUCCESS) {
								var ref = $('#jstree').jstree(true)
								//删除角色节点
								ref.delete_node(treeNodeId);

								//角色权限树销毁
								if ($('#resourceTree').jstree(true)) { //销毁当前资源权限树，
									$('#resourceTree').jstree(true).destroy();
								}

							} else {
								handleAjaxError(response);
							}
						}
					});
				}
			});
		}

		/**
		 * 初始化角色表单校验规则
		 *       新增、修改角色时
		 */
		function initFormValidator() {
			$('#roleForm').validate({
				rules : {
					'role.id' : {},
					'role.role' : {
						required : true,
						maxlength : 20
					},
					'role.organization_id' : {
						required : true
					},
					'role.description' : {
						required : false,
						maxlength : 100
					}
				}
			});
		}
	</script>
</body>
</html>